#!/bin/sh

#This shell scripts used to update the u-boot linux kernel, root file system image when Linux is running. by richard

IMG_DIR=./img
CURDATE=`date +%Y%m%d`
erase_cmd=/usr/sbin/flash_erase

write_cmd=/usr/sbin/nandwrite

MLO_PART=mtd0
MLO_PART_BLKSIZE=1
UBOOT_PART=mtd5
UBOOT_PART_BLKSIZE=4
DTS_PART=mtd4
DTS_PART_BLKSIZE=2
KERNEL_PART=mtd8
KERNEL_PART_BLKSIZE=32
ROOTFS_PART=mtd9
ROOTFS_PART_BLKSIZE=1968

update_flag=0

#Notice!!
#if your change this function,you need update updatescript.sh sd_log(),in minirootfs /usr/local/qt5.6/app

update_log() {
    echo $1
    mkdir -p ./log
    datetime=`date "+%Y-%m-%d %T"`
    runningfile=`basename $0`
    echo [$runningfile $datetime] $1 >> ./log/log_$CURDATE.txt

}
check_ret() {
		if [ $1 -eq 0 ];then
		  update_log "success!"
		else
		  update_flag=-1
		  update_log "faild!"
fi
}

burn_image()

{

    partition=$1
    file_name=$2
    erase_size=$3

    if ( ! $erase_cmd /dev/$partition 0x0 $erase_size ) ; then

        update_log "Erase /dev/$partition failure."

        return -1

    fi

    if ( ! $write_cmd -p /dev/$partition $file_name ) ; then

        update_log "Write $file_name to /dev/$partition failure."

        return -1

    fi

    return 0
}

update_log "Start running $0"
if [ $# -lt 1 ];then
    update_log "Usage: need MLO,uboot,dts,kernel,rootfs"
    exit 1
fi

for OPT in $@
do
    if [ $update_flag -eq -1 ];then
	update_log "update_flag = -1"
	break
    fi
    case $OPT in
        MLO)
            if [ -f $IMG_DIR/MLO_NAND ];then
                update_log "Update MLO"
                burn_image $MLO_PART $IMG_DIR/MLO_NAND $MLO_PART_BLKSIZE
		check_ret $?
            else
                update_log "can't find $IMG_DIR/MLO_NAND "
		update_flag=-1
            fi
            ;;
        uboot)
            if [ -f $IMG_DIR/u-boot_NAND.img ];then
                update_log "Update uboot"
                burn_image $UBOOT_PART $IMG_DIR/u-boot_NAND.img $UBOOT_PART_BLKSIZE
		check_ret $?
            else
                update_log "can't find $IMG_DIR/u-boot_NAND.img "
		update_flag=-1
            fi
            ;;
        dts)
            if [ -f $IMG_DIR/am335x-evmsk.dtb ];then
                update_log "Update dts"
                burn_image $DTS_PART $IMG_DIR/am335x-evmsk.dtb $DTS_PART_BLKSIZE
		check_ret $?
            else
                update_log "can't find $IMG_DIR/am335x-evmsk.dtb "
		update_flag=-1
            fi
            ;;
        kernel)
            if [ -f $IMG_DIR/uImage ];then
                update_log "Update kernel"
                burn_image $KERNEL_PART $IMG_DIR/uImage $KERNEL_PART_BLKSIZE
		check_ret $?
            else
                update_log "can't find $IMG_DIR/uImage "
		update_flag=-1
            fi
            ;;
        rootfs)
            ;;
        *)
            update_log "Error: unknow update option. MLO,uboot,dts,kernel,rootfs!"
            ;;
    esac
done
	    sync
	    sync
if [ $update_flag -eq 0 ];then
	    update_log "update success, system restart!"
	    sync
	    sync
	    sync
	    sync
	    sync
	    sync
 /sbin/shutdown -r now
fi
